package com.cqeec.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JOptionPane;

import com.cqeec.dao.TeacherDao;
import com.cqeec.domain.Teacher; 

public class TeacherDaoImp implements TeacherDao {
	private PreparedStatement prst;

	@Override
	public Teacher findTeacherByID(Connection conn, String teacherNum) {
		Teacher teacher=new Teacher();
		String sql = "SELECT * from teacher where teacherNum= ?";
		 PreparedStatement prepare=null;
		try {
			 prepare = conn.prepareStatement(sql);
			 prepare.setString(1, teacherNum);
			 ResultSet rs = prepare.executeQuery();
			 if(rs.next()) {
				 teacher.setTeacherName(rs.getString("teacherName").trim());
				 teacher.setTeacherNum(rs.getString("teacherNum").trim());
				 teacher.setTeacherPwd(rs.getString("teacherPwd").trim());
				 teacher.setCourseNum(rs.getString("courseNum").trim());
				 teacher.setTeacherMajor(rs.getString("teacherMajor").trim());
				 
				 
				
				 return teacher;
			 }
		
		} catch (SQLException e) { 
			JOptionPane.showMessageDialog(null, "查询失败请联系管理员", "数据错误", JOptionPane.ERROR_MESSAGE);
			return null;
		} 
		return null;
	}



	@Override
	public int addTeacherByID(Connection conn, Teacher teacher) {
		String sql="INSERT INTO teacher VALUES(?,?,?,?,?)";
		  try {
			prst=conn.prepareStatement(sql);
			prst.setString(1, teacher.getTeacherName());
			prst.setString(2, teacher.getTeacherNum());
			prst.setString(3, teacher.getTeacherPwd());
			prst.setString(4, teacher.getTeacherMajor());
			prst.setString(5, teacher.getCourseNum());
			int rs=prst.executeUpdate();
			if(rs!=0) {
				return rs;
			}
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "添加失败请联系管理员", "数据错误", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
			e.printStackTrace();
		}
		return 0;
	}
 
	@Override
	public void upateTeacherByTeacherNum(Connection conn, Teacher teacher) throws Exception { 
		String sql = "update teacher set teacherPwd = ? WHERE teacherNum = ?";
		
		PreparedStatement psmt = conn.prepareStatement(sql);
		psmt.setString(1, teacher.getTeacherPwd());
		psmt.setString(2, teacher.getTeacherNum());
		psmt.executeUpdate();
		
	}



	@Override
	public ResultSet getallleave(Connection con) {
		 ResultSet rs=null;
		  try {
			 String sql="select * from leaved";
			  PreparedStatement prst=con.prepareStatement(sql);
			 rs = prst.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("数据库连接失败");
			e.printStackTrace();
		}
		 
		
		
		
		
		
	

		return rs;
	}

}
